using System;
using BruteForce.NewManagers;
using BruteForce.UserInterface.Abstracts;
using BruteForce.UserInterface.UIControls;
using BruteForce.Utilities;
using Bruteforce.UserInterface.UIControls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BruteForce.Screens.CreditsScreen.Panels
{
    public class CreditsPanel : AbstractPanel {
        private int _currentUIControlIndex;
        private int _currentPanelTexture = 0;


        public CreditsPanel()
            : base("Instructions")
        {

        }

        public override void Update(GameTime gameTime) {
            foreach (AbstractUIControl uiControl in Children)
                uiControl.Update();
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) {

            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap,
                              DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(UIControlManager.Instance.BlackStoneTexture, new Vector2(Bounds.X, Bounds.Y), Bounds,
                             Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.End();
            spriteBatch.Begin();
            Draws.DrawBorder(Bounds, new Color(91, 91, 91), 2);


            foreach (AbstractUIControl UIcontrol in Children) {
                UIcontrol.Draw(gameTime);
            }
        }

        public override void AfterLayout()
        {
            var diffBetweenUIcomponets = 10;
            var hightDiff = UIControlManager.Instance.StartNewGameButton.Height + diffBetweenUIcomponets;

            var centerRectangle =CalculteCenteredRectangle(Bounds,
                                      UIControlManager.Instance.StartNewGameButton.Width,
                                      UIControlManager.Instance.StartNewGameButton.Height);

            //help image
            var img = UIControlManager.Instance.CreditsImg;
            var rectangle = new Rectangle((Bounds.Width - img.Width) / 2, (Bounds.Height - img.Height) / 2,
                                          img.Width, img.Height);
            var dynamicImg = new Image(img, "img", rectangle);
            dynamicImg.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(dynamicImg);

           // exit
            img = UIControlManager.Instance.CloseButton;
            var button = CreateButton(new Rectangle(rectangle.Width + rectangle.X- img.Width-15, rectangle.Y + 10, img.Width, img.Height), img);
            button.OnClick += (() => ScreenManager.Instance.PopScreen());
            
        }

    private Button CreateButton(Rectangle centerRectangle, Texture2D image)
        {
                
                var abstractMenuItem = new Button(image, "Create Button",
                                                  new Rectangle(centerRectangle.X,
                                                                centerRectangle.Y, image.Width,
                                                                image.Height));
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                AddUIControl(abstractMenuItem);
            return abstractMenuItem;
        }
    }
}